package com.ulic.customerManApplication.util.message;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import com.ulic.smscenter.webservices.SmsSendClient;

public class MessageUtil {

	public String directSendMessage(String mobileCell, String messageValue,
			String smsCode) throws Exception {
		String smsReturnResultFlag = "0";
		String isSuccess = "Y";
		try {
			StringBuffer result = new StringBuffer();
			result.append(SmsSendClient.combineMessageHeader("13", smsCode,
					dateToStr(new java.util.Date())));
			result.append(SmsSendClient.combineMessageMiddle(mobileCell,
					messageValue));
			result.append(SmsSendClient.combineMessageFinality());
			String smsReturnResult = SmsSendClient.sendMessage(result
					.toString());
			System.out.println(smsReturnResult);
			smsReturnResultFlag = smsReturnResult.substring(
					smsReturnResult.indexOf("<RESULT>") + 8,
					smsReturnResult.indexOf("<RESULT>") + 9);
			if (smsReturnResultFlag.equals("0")) {
				isSuccess = "N";
			}

		} catch (Exception exception) {
			smsReturnResultFlag = "0";
			isSuccess = "N";
			exception.printStackTrace();
		}
		return isSuccess;

	}
	public String directSendMessage(List<String> mobileCells, String messageValue,
			String smsCode) throws Exception {
		String smsReturnResultFlag = "0";
		String isSuccess = "Y";
		try {
			StringBuffer result = new StringBuffer();
			result.append(SmsSendClient.combineMessageHeader("13", smsCode,
					dateToStr(new java.util.Date())));
			result.append(SmsSendClient.combineMessageMiddle(mobileCells,
					messageValue));
			result.append(SmsSendClient.combineMessageFinality());
			String smsReturnResult = SmsSendClient.sendMessage(result
					.toString());
			System.out.println(smsReturnResult);
			smsReturnResultFlag = smsReturnResult.substring(
					smsReturnResult.indexOf("<RESULT>") + 8,
					smsReturnResult.indexOf("<RESULT>") + 9);
			if (smsReturnResultFlag.equals("0")) {
				isSuccess = "N";
			}

		} catch (Exception exception) {
			smsReturnResultFlag = "0";
			isSuccess = "N";
			exception.printStackTrace();
		}
		return isSuccess;

	}
	public static String dateToStr(java.util.Date date) throws Exception {
		try {
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			return dateFormat.format(date);
		} catch (Exception ex) {
			throw ex;
		}
	}

	/**
	 * @param args
	 * @throws Exception 
	 */

}
